package org.skatiger.jrove.component.html;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.NamingContainer;
import org.skatiger.jrove.component.UIComponent;
import org.skatiger.jrove.component.foreach.ForeachComponent;
import org.skatiger.jrove.component.foreach.ForeachStatusIterator;
import org.skatiger.jrove.component.foreach.ForeachUtil;

public class HtmlDataTable extends HtmlComponentBase implements
        ForeachComponent, NamingContainer {
    private static final String DEFAULT_DATA_TABLE_STYLE_CLASS = "data_table";
    public static final String CAPTION_FACET_NAME = "caption";
    public static final String ITEMS_PROPERTY_NAME = "items";
    protected Object items;
    protected String var;
    protected String varStatus;
    protected Integer begin;
    protected Integer end;
    protected Integer step;
    protected String rowClasses;
    protected ForeachStatusIterator _iterator;

    public void setBegin(Integer begin) {
        this.begin = begin;
    }

    public Integer getBegin() {
        return getPropertyValue("begin", begin);
    }

    public void setEnd(Integer end) {
        this.end = end;
    }

    public Integer getEnd() {
        return getPropertyValue("end", end);
    }

    public void setStep(Integer step) {
        this.step = step;
    }

    public Integer getStep() {
        return getPropertyValue("step", step);
    }

    @Override
    public String getStyleClass() {
        return getPropertyValue("styleClass", styleClass,
                DEFAULT_DATA_TABLE_STYLE_CLASS);
    }

    public UIComponent getCaption() {
        return getFacet(CAPTION_FACET_NAME);
    }

    public void setRowClasses(String rowClasses) {
        this.rowClasses = rowClasses;
    }

    public String getRowClasses() {
        return getPropertyValue("rowClasses", rowClasses);
    }

    public void setItems(Object items) {
        this.items = items;
    }

    public Object getItems() {
        return getPropertyValue("items", this.items);
    }

    public void setVar(String var) {
        this.var = var;
    }

    public String getVar() {
        return getPropertyValue("var", this.var);
    }

    public void setVarStatus(String varStatus) {
        this.varStatus = varStatus;
    }

    public String getVarStatus() {
        return getPropertyValue("varStatus", varStatus);
    }

    //    public String getCurrentRowClasses(int current) {
    //        if (rowClassArray == null) {
    //            String rowClasses = getRowClasses();
    //            if (rowClasses != null) {
    //                rowClassArray = rowClasses.split(",");
    //            } else {
    //                rowClassArray = new String[] { "" };//EMPTY classes
    //            }
    //        }
    //        String currentRowClass = rowClassArray[0];
    //        if (rowClassArray.length > 1) {
    //            int idx = current % rowClassArray.length;//current start with 0
    //            currentRowClass = rowClassArray[idx];
    //        }
    //        return currentRowClass;
    //    }

    //    public void capturePreviousValue() {
    //        if (this.var != null || this.varStatus != null) {
    //            ViewContext viewContext = getViewContext();
    //            Map<String, Object> attrs = viewContext.getAttributes();
    //            if (this.var != null) {
    //                this.previousVar = attrs.get(this.var);
    //            }
    //            if (this.varStatus != null) {
    //                this.previousVarStatus = attrs.get(this.varStatus);
    //            }
    //        }
    //    }
    //
    //    public void restorePreviousValue() {
    //        if (this.var != null || this.varStatus != null) {
    //            ViewContext viewContext = getViewContext();
    //            Map<String, Object> attrs = viewContext.getAttributes();
    //            if (this.var != null) {
    //                if (this.previousVar != null) {
    //                    attrs.put(this.var, this.previousVar);
    //                } else {
    //                    attrs.remove(this.var);
    //                }
    //            }
    //            if (this.varStatus != null) {
    //                if (this.previousVarStatus != null) {
    //                    attrs.put(this.varStatus, this.previousVarStatus);
    //                } else {
    //                    attrs.remove(this.varStatus);
    //                }
    //            }
    //        }
    //    }

    public ForeachStatusIterator getForeachStatusIterator() {
        if (this._iterator == null) {
            this._iterator = ForeachUtil.iterate(getItems(), getBegin(),
                    getEnd(), getStep());
        }
        return this._iterator;
    }
    public String getContainerClientId(ViewContext context) {
        String clientId = super.getContainerClientId(context);
        int rowIndex = getForeachStatusIterator().getStatus().getIndex();
        if (rowIndex == -1) {
            return clientId;
        }
        StringBuilder bld = __getSharedStringBuilder();
        return bld.append(clientId).append(NamingContainer.SEPARATOR_CHAR)
                .append(rowIndex).toString();
    }

}
